package nl.zuiderfestival.raydance.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import nl.zuiderfestival.raydance.control.RayDanceListener;
import nl.zuiderfestival.raydance.model.RayDance;
import nl.zuiderfestival.raydance.model.RayEvent;
import nl.zuiderfestival.raydance.model.RayEventType;
import nl.zuiderfestival.raydance.model.zones.Zone;
import nl.zuiderfestival.raydance.view.canvas.ZoneDisplay;
import nl.zuiderfestival.raydance.view.particle.TuioParticleDisplay;

public class RayDanceFrame extends JFrame implements Observer {

	private static final long serialVersionUID = 1L;

	private final int WIDTH = 800;
	private final int HEIGHT = 600;

	private ConnectionPanel connectionPanel;
	private ZonePanel zonePanel;
	private TuioParticleDisplay particleDisplay;

	private int scale;
	
	public RayDanceFrame(int scale) throws HeadlessException {
		super("RayDance");
		this.scale = scale;
		
		try  {
		  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
		  System.out.println("Error setting native LAF: " + e);
		}
		this.setVisible(true);
		
		createContentPane();
		
		this.pack();
		centerWindow();
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public void addRayDanceListener(RayDanceListener l) {
		zonePanel.addRayDanceListener(l);
		connectionPanel.addActionListener(l);
		particleDisplay.addActionListener(l);
	}
	
	private void createContentPane() {
		connectionPanel = new ConnectionPanel();
		zonePanel = new ZonePanel();
		particleDisplay = new TuioParticleDisplay(scale);
	    
		JPanel pane = new JPanel();
		pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
		pane.add(connectionPanel);
		pane.add(zonePanel);
		
		JPanel overlayPanel = new JPanel();
		
		JPanel canvasPanel = new JPanel(new BorderLayout());
		canvasPanel.setPreferredSize(new Dimension(scale, scale));
		canvasPanel.add(particleDisplay, BorderLayout.CENTER);

		overlayPanel.add(canvasPanel);
		
		pane.add(overlayPanel);
		
		getContentPane().add(pane);
	}
	
	private void centerWindow() {
		Dimension screen = getToolkit().getScreenSize();
		int x = screen.width / 2 - WIDTH / 2;
		int y = screen.height / 2 - HEIGHT / 2;
		this.setLocation(x, y);
	}

	public Observer getZonePanel() {
		return zonePanel;
	}

	public ZoneDisplay getZoneDisplay() {
		return particleDisplay;
	}

	public ConnectionPanel getConnectionPanel() {
		return connectionPanel;
	}

	public TuioParticleDisplay getTuioParticleDisplay() {
		return particleDisplay;
	}

	public Zone getSelectedZone() {
		return zonePanel.getSelectedZone();
	}

	public Zone getSelectedShapeZone() {
		return particleDisplay.getSelectedItem().getZone();
	}

	public void update(Observable o, Object arg) {
		if (o instanceof RayDance) {
			if (arg instanceof RayEvent) {
				RayEvent event = (RayEvent) arg;
				if (event.getType().equals(RayEventType.INIT)) {
					particleDisplay.start();
				}
			}
		}
	}

	public void toggleMovementDisplay() {
		particleDisplay.toggleDisplayMovementEnabled();
	}

}
